package com.jiayinlending.jiayindai.util;

import android.content.Context;
import android.os.IBinder;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by xiaohei on 2017/4/19.
 */

public class SoftKeyboardManager {
    private SoftKeyboardManager() {
    }

    private static SoftKeyboardManager softKeyboardManager;

    public static SoftKeyboardManager getStance() {
        if (null == softKeyboardManager) {
            softKeyboardManager = new SoftKeyboardManager();
        }
        return softKeyboardManager;
    }

    private InputMethodManager inputMethodManager;

    /**
     * 隐藏软键盘
     *
     * @param context
     * @param iBinder
     */
    public void hidenSoftKeyboard(Context context, IBinder iBinder) {
        if (null == inputMethodManager) {
            inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        inputMethodManager.hideSoftInputFromWindow(iBinder, 0);
    }

    /**
     * 显示软键盘
     */
    public void showSoftKeyboard(Context context, IBinder iBinder) {
        if (null == inputMethodManager) {
            inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        inputMethodManager.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }


    public static void closeBoard(Context mcontext) {
        InputMethodManager imm = (InputMethodManager) mcontext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive())
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);

    }

    public static void hideSystemKeyBoard(Context mcontext, View v) {
        InputMethodManager imm = (InputMethodManager) mcontext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}
